package com.hczt.xhminiapp.common.bean;

import lombok.Getter;

/**
 * @author 红创-马海强
 * @date 2019-03-07 17:18
 * @description
 */
@Getter
public class CodeMsg<T> {
    private int code;
    private String message;
    private T data;

    private CodeMsg(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private CodeMsg(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 通用模块 5001XX
    public static CodeMsg SUCCESS = new CodeMsg(0, "操作成功");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常：%s");
    public static CodeMsg UPLOAD_FAILED = new CodeMsg(500102, "文件上传失败");
    public static CodeMsg DECRYPTION_DATA_ERROR = new CodeMsg(500103,"数据解密失败: %s");
    public static CodeMsg METHOD_NOT_SUPPORT = new CodeMsg(500104, "Request method '%s' not supported");
    public static String OPERATE_SUCCESS = "操作成功";

    // OAuth2 验证模块 4001XX
    public static CodeMsg OAUTH2_UNKNOWN_ERROR = new CodeMsg(400100, "%s");
    public static CodeMsg OAUTH2_INVALID_TOKEN = new CodeMsg(400101, "无效Token:%s");
    public static CodeMsg OAUTH2_ACCESS_DENIED = new CodeMsg(400102, "无权访问:%s");
    public static CodeMsg OAUTH2_INVALID_GRANT = new CodeMsg(400103, "用户名或密码错误");
    public static CodeMsg OAUTH2_UNSUPPORTED_GRANT_TYPE = new CodeMsg(400104, "不支持的授权类型:%s");
    public static CodeMsg OAUTH2_INVALID_CLIENT = new CodeMsg(400105, "错误的客户端凭证");
    public static CodeMsg SYSUSER_OLD_PASSWORD_INVALID = new CodeMsg(400106, "旧密码错误:%s");


    public static CodeMsg LOGIN_WX_ERROR = new CodeMsg(500200, "获取微信access_token错误");
    public static CodeMsg NEED_LOGIN = new CodeMsg(500201, "请先登录");
    public static CodeMsg OPENID_ROLE_NO_MATCH = new CodeMsg(500203, "参数匹配错误，没有用户信息");
    public static CodeMsg ROLE_ERROR = new CodeMsg(500204, "没有可以匹配角色");

    public static CodeMsg ACCESS_DENIED = new CodeMsg(600100, "认证失效，请重新登录!");
    public static CodeMsg UNAUTHORIZED = new CodeMsg(600100, "用户没有权限");
    public static CodeMsg PASSWORD_ERROR = new CodeMsg(600101, "用户名或密码错误");
    public static CodeMsg USER_EXISTS = new CodeMsg(600102, "用户名已存在");

    /**
     * 带参数的CodeMsg
     *
     * @param args 待填充的参数
     * @return 组装好的CodeMsg
     */
    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.message, args);
        return new CodeMsg(code, message);
    }

    public CodeMsg<T> addData(T data) {
        int code = this.code;
        String message = this.message;
        return new CodeMsg(code, message, data);
    }
}
